package com.mylib.libcore;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatDelegate;

import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.config.AppConfig;

import com.mylib.libcore.utils.AppPreferenceForever;
import com.mylib.libcore.utils.AppPreferencesHelper;
import com.mylib.libcore.utils.LoadViewHelper;
import com.mylib.libcore.utils.ToastUtil;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.tencent.smtt.sdk.QbSdk;
/**
 * @time 2018-08-16 10:38
 */
public class AppContext {

    private static Application mApplication;

    public boolean mIsForeground = false;

    public boolean mIsBackToForeground = false;

    private static AppContext instance;

    private AppPreferencesHelper appPreferencesHelper;

    private AppPreferenceForever appPreferenceForever;

    public static AppContext getInstance() {
        synchronized (AppContext.class) {
            if (null == instance) {
                instance = new AppContext();
            }
        }

        return instance;
    }

    public void init(Application application) {
        mApplication = application;

        // 部分机型中兼容vector图片
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);


        //x5内核初始化接口
        QbSdk.initX5Environment(application, new QbSdk.PreInitCallback() {
            @Override
            public void onCoreInitFinished() {
                Logger.d(" onCoreInitFinished");
            }

            @Override
            public void onViewInitFinished(boolean b) {
                Logger.d(" onViewInitFinished is " + b);
            }
        });

        initLogger();
        initArouter();
        initLoadingHelper();
        registerActivityLifecycleCallbacks(application);
        ToastUtil.init(application);
    }



    public AppPreferencesHelper getAppPreferencesHelper() {
        if (appPreferencesHelper == null) {
            appPreferencesHelper = new AppPreferencesHelper(getContext());
        }
        return appPreferencesHelper;
    }

    public AppPreferenceForever getAppPreferenceForever() {
        if (appPreferenceForever == null) {
            appPreferenceForever = new AppPreferenceForever(getContext());
        }
        return appPreferenceForever;
    }


    public Context getContext() {
        return mApplication.getApplicationContext();
    }


    private void initArouter() {
        if (BuildConfig.DEBUG) {
            ARouter.openLog();
            ARouter.openDebug();
        }
        ARouter.init(mApplication);
    }



    /**
     * 初始化加载界面，空界面等
     */
    private void initLoadingHelper() {
        LoadViewHelper.getBuilder()
                .setLoadDefault(R.layout.default_view)
                .setLoadEmpty(R.layout.empty_view)
                .setLoadError(R.layout.error_view)
                .setLoadIng(R.layout.loading_view);
    }

    private void initLogger() {
        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .tag(AppConfig.LOGGER_TAG)
                .build();
        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy));
    }

    private void registerActivityLifecycleCallbacks(Application application) {
        application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {

            private int refCount = 0;

            @Override
            public void onActivityCreated(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityStarted(Activity activity) {

                if (refCount == 0) {
                    mIsBackToForeground = true;
                } else {
                    mIsBackToForeground = false;
                }
                refCount++;
                mIsForeground = true;
            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {
                refCount--;
                if (refCount == 0) {
                    mIsForeground = false;
                }
            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
            }
        });
    }
}
